package com.test.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @ClassName KongController
 * @Description
 * @Author Zhang Li Tao
 * @Date 2023/6/20 10:58
 * @Version 1.0
 **/
@RestController
public class KongController {
    @GetMapping("status")
    public JSONObject status() {
        return JSONUtil.parseObj("{\"database\":{\"reachable\":true},\"memory\":{\"workers_lua_vms\":[{\"http_allocated_gc\":\"0.13 MiB\",\"pid\":70},{\"http_allocated_gc\":\"0.13 MiB\",\"pid\":71},{\"http_allocated_gc\":\"0.11 MiB\",\"pid\":72},{\"http_allocated_gc\":\"0.07 MiB\",\"pid\":73},{\"http_allocated_gc\":\"0.07 MiB\",\"pid\":74},{\"http_allocated_gc\":\"0.11 MiB\",\"pid\":75},{\"http_allocated_gc\":\"0.08 MiB\",\"pid\":76},{\"http_allocated_gc\":\"0.06 MiB\",\"pid\":77}],\"lua_shared_dicts\":{\"kong_locks\":{\"allocated_slabs\":\"0.06 MiB\",\"capacity\":\"8.00 MiB\"},\"kong\":{\"allocated_slabs\":\"0.04 MiB\",\"capacity\":\"5.00 MiB\"},\"kong_process_events\":{\"allocated_slabs\":\"0.95 MiB\",\"capacity\":\"5.00 MiB\"},\"kong_db_cache\":{\"allocated_slabs\":\"1.34 MiB\",\"capacity\":\"128.00 MiB\"},\"kong_cluster_events\":{\"allocated_slabs\":\"0.04 MiB\",\"capacity\":\"5.00 MiB\"},\"kong_db_cache_miss\":{\"allocated_slabs\":\"0.08 MiB\",\"capacity\":\"12.00 MiB\"},\"prometheus_metrics\":{\"allocated_slabs\":\"2.31 MiB\",\"capacity\":\"64.00 MiB\"},\"kong_healthchecks\":{\"allocated_slabs\":\"0.20 MiB\",\"capacity\":\"5.00 MiB\"},\"kong_rate_limiting_counters\":{\"allocated_slabs\":\"0.08 MiB\",\"capacity\":\"12.00 MiB\"}}},\"server\":{\"connections_writing\":2,\"total_requests\":21656224,\"connections_handled\":20771986,\"connections_accepted\":20771986,\"connections_reading\":0,\"connections_active\":4,\"connections_waiting\":2}}");
    }

    @GetMapping("metrics")
    public void metrics(HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        String s = "# HELP kong_bandwidth Total bandwidth in bytes consumed per service/route in Kong\n" +
                "# TYPE kong_bandwidth counter\n" +
                "kong_bandwidth{type=\"egress\",service=\"service_016b4b9eef484e1b96dc73e2d2b844ac\",route=\"fe13493f-b341-43da-a83a-6308162f5a03\"} 299772\n" +
                "kong_bandwidth{type=\"ingress\",service=\"service_fa00f04f5bc540139c4bd2bf95c5ecb4\",route=\"ef64145c-2b55-45b7-a346-777b94204129\"} 341921\n" +
                "kong_bandwidth{type=\"ingress\",service=\"service_fb0a734ecd9e4c9d9ee5570cedb19464\",route=\"0db0a2cd-ff12-460c-85a0-4cb70f6403aa\"} 3068814811\n" +
                "kong_bandwidth{type=\"ingress\",service=\"service_fd71d58710fd43b9bf2cb55ea225992a\",route=\"7f580da4-21e2-4ed6-afa1-68bb5ad4f0b2\"} 1509314\n" +
                "# HELP kong_datastore_reachable Datastore reachable from Kong, 0 is unreachable\n" +
                "# TYPE kong_datastore_reachable gauge\n" +
                "kong_datastore_reachable 1\n" +
                "# HELP kong_http_status HTTP status codes per service/route in Kong\n" +
                "# TYPE kong_http_status counter\n" +
                "kong_http_status{code=\"200\",service=\"service_016b4b9eef484e1b96dc73e2d2b844ac\",route=\"fe13493f-b341-43da-a83a-6308162f5a03\"} 489\n" +
                "kong_http_status{code=\"200\",service=\"service_f535e23dfa9f43aa873c8c237a7ae99d\",route=\"74dc1d59-a8d4-44b9-951d-ad162f1d6a33\"} 16758\n" +
                "kong_http_status{code=\"200\",service=\"service_f6fe467618f64d2f8ce315a4cdba3c37\",route=\"b462063e-4696-47ef-a659-11bdfc8abda1\"} 42\n" +
                "kong_http_status{code=\"404\",service=\"service_bd95913685444fc3b93065d4c9f537d3\",route=\"bb49a62a-fc52-4779-b481-319355ee2155\"} 26336\n" +
                "kong_http_status{code=\"499\",service=\"service_117b8398224f438d8753627389794457\",route=\"df31c90c-34a4-4507-8549-1a3feeb378e7\"} 2\n" +
                "kong_http_status{code=\"499\",service=\"service_1aa46f41e3c04683b0e13378abd8f2cc\",route=\"5d5c4351-0a4b-45ad-ad6c-0100d8f08271\"} 1\n" +
                "kong_http_status{code=\"499\",service=\"service_469ed802f7c94f84b94fb11195a10377\",route=\"5bb2d0e4-7114-4743-8afb-7b726848a05d\"} 1\n" +
                "kong_http_status{code=\"499\",service=\"service_4e75a3696a16499ea27464517d6b2b16\",route=\"2499d8cf-f2ca-48e6-ac47-8d86db5015bc\"} 22\n" +
                "kong_http_status{code=\"499\",service=\"service_566a17dfabac45dc8c2bd7427bad12a0\",route=\"85caac8f-ef6e-4783-ba15-c040555c10d7\"} 328\n" +
                "kong_http_status{code=\"499\",service=\"service_6e8e4750a72442f8904b6ae698f2fa9f\",route=\"51f0f86f-48f6-4c47-b7e6-c7badd388485\"} 6\n" +
                "kong_http_status{code=\"499\",service=\"service_76e617c4564546cb91cea83dfe7c22b4\",route=\"74dc366a-3655-4064-a91c-74e1fbf9693d\"} 14\n" +
                "kong_http_status{code=\"499\",service=\"service_fb0a734ecd9e4c9d9ee5570cedb19464\",route=\"0db0a2cd-ff12-460c-85a0-4cb70f6403aa\"} 2\n" +
                "kong_http_status{code=\"500\",service=\"service_679064e545eb427880cb6b3e357fc070\",route=\"0bff64c7-7df8-4429-822e-5d8657b5dd28\"} 1\n" +
                "kong_http_status{code=\"504\",service=\"service_0793cbb17e2449be94817f86cb41dd4d\",route=\"f941fe3c-8d5f-4ecb-bf6a-14c16eaac493\"} 2\n" +
                "# HELP kong_memory_lua_shared_dict_bytes Allocated slabs in bytes in a shared_dict\n" +
                "# TYPE kong_memory_lua_shared_dict_bytes gauge\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong\"} 40960\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_cluster_events\"} 40960\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_db_cache\"} 1400832\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_db_cache_miss\"} 86016\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_healthchecks\"} 208896\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_locks\"} 61440\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_process_events\"} 995328\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"kong_rate_limiting_counters\"} 86016\n" +
                "kong_memory_lua_shared_dict_bytes{shared_dict=\"prometheus_metrics\"} 2420736\n" +
                "# HELP kong_memory_lua_shared_dict_total_bytes Total capacity in bytes of a shared_dict\n" +
                "# TYPE kong_memory_lua_shared_dict_total_bytes gauge\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong\"} 5242880\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_cluster_events\"} 5242880\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_db_cache\"} 134217728\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_db_cache_miss\"} 12582912\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_healthchecks\"} 5242880\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_locks\"} 8388608\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_process_events\"} 5242880\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"kong_rate_limiting_counters\"} 12582912\n" +
                "kong_memory_lua_shared_dict_total_bytes{shared_dict=\"prometheus_metrics\"} 67108864\n" +
                "# HELP kong_memory_workers_lua_vms_bytes Allocated bytes in worker Lua VM\n" +
                "# TYPE kong_memory_workers_lua_vms_bytes gauge\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"70\"} 147963\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"71\"} 73760\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"72\"} 112849\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"73\"} 73097\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"74\"} 76367\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"75\"} 110936\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"76\"} 79695\n" +
                "kong_memory_workers_lua_vms_bytes{pid=\"77\"} 67761\n" +
                "# HELP kong_nginx_http_current_connections Number of HTTP connections\n" +
                "# TYPE kong_nginx_http_current_connections gauge\n" +
                "kong_nginx_http_current_connections{state=\"accepted\"} 20772150\n" +
                "kong_nginx_http_current_connections{state=\"active\"} 3\n" +
                "kong_nginx_http_current_connections{state=\"handled\"} 20772150\n" +
                "kong_nginx_http_current_connections{state=\"reading\"} 0\n" +
                "kong_nginx_http_current_connections{state=\"total\"} 21656389\n" +
                "kong_nginx_http_current_connections{state=\"waiting\"} 1\n" +
                "kong_nginx_http_current_connections{state=\"writing\"} 2\n" +
                "# HELP kong_nginx_metric_errors_total Number of nginx-lua-prometheus errors\n" +
                "# TYPE kong_nginx_metric_errors_total counter\n" +
                "kong_nginx_metric_errors_total 0";

        List<String> split = StrUtil.split(s, "\n");

        for (String row : split) {
            writer.println(row);
        }

        writer.close();
    }

}
